﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CameraProject2
{
	public partial class Form1 : Form
	{
		private bool working = false;
        // private bool grab = false;

		public Form1()
		{
			InitializeComponent();

            this.Load += new EventHandler(Form1_Load);
            
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
		}

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        void Form1_Load(object sender, EventArgs e)
        {
        }

        // Обработчик события "С камеры пришел новый кадр"
        void WebCamControl21_OnNewFrame(object myObject, WebCamControl.WebCamControlArgs myArgs)
        {
            if (working)
            {
                #region сохранение картинки через булеву переменную
                /*
                if (grab)
                {
                    string fileName = String.Format("Pic {0}-{1}-{2}.JPG", DateTime.Now.ToShortDateString(),
                                DateTime.Now.ToLongTimeString(), DateTime.Now.Millisecond).Replace(':', '.');
                    fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), fileName);

                    myArgs.LastFrame.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    grab = false;
                }
               */

                #endregion
                // обработать myArgs.LastFrame
            }
        }

		#region Начать показывать видео в окошке
		private void buttonBegin_Click(object sender, EventArgs e)
		{
			WebCamControl21.CaptureVideo();
			this.working = true;
			WebCamControl21.Refresh();

            WebCamControl21.OnNewFrame += new WebCamControl.WebCamControl.NewFrameHandler(WebCamControl21_OnNewFrame);

			buttonCapturePic.Enabled = buttonEnd.Enabled = buttonRecStart.Enabled = true;
			buttonBegin.Enabled = false;			
		} 
		#endregion
		
		#region Прекратить показывать видео
		private void buttonEnd_Click(object sender, EventArgs e)
		{
            WebCamControl21.OnNewFrame -= new WebCamControl.WebCamControl.NewFrameHandler(WebCamControl21_OnNewFrame);
            this.working = false;
            WebCamControl21.StopCapture();			         

			buttonCapturePic.Enabled = buttonEnd.Enabled = buttonRecStart.Enabled = buttonRecStop.Enabled = false;
			buttonBegin.Enabled = true;
		} 
		#endregion

		#region Сохранить изображение с видеопотока
		private void buttonCapturePic_Click(object sender, EventArgs e)
		{
            if (working)
            {
                buttonCapturePic.Enabled = false;
                buttonCapturePic.BackColor = Color.Green;

                WebCamControl21.GrabImage();
                //grab = true;

                buttonCapturePic.Enabled = true;
                buttonCapturePic.BackColor = SystemColors.Control;
            }
		} 
		#endregion

		#region Начать запись видео
		private void buttonRecStart_Click(object sender, EventArgs e)
		{
			buttonRecStart.Enabled = buttonEnd.Enabled = false;
			buttonRecStart.BackColor = Color.Red;
			buttonRecStop.Enabled = true;

            if (working)
                WebCamControl21.RecordVideoStart();
		} 
		#endregion

		#region Прекратить запись видео
		private void buttonRecStop_Click(object sender, EventArgs e)
		{
			buttonRecStop.Enabled = false;
			buttonRecStart.Enabled = buttonEnd.Enabled = true;
			buttonRecStart.BackColor = SystemColors.Control;

            if (working)
                WebCamControl21.RecordVideoStop();
		} 
		#endregion

	}
}
